package com.guoguo.service.impl;


import com.github.pagehelper.PageHelper;
import com.guoguo.base.PageBean;
import com.guoguo.base.PaginationContext;

import com.guoguo.entity.Favorite;
import com.guoguo.mapper.FavoriteMapper;
import com.guoguo.service.UserFavouriteService;
import com.guoguo.entity.vo.UserFavouriteVO;
import com.guoguo.mapper.UsersMapper;
import com.guoguo.utils.AliOssUtils;
import com.guoguo.utils.aliOss.AliOssImageConfig;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/8/17.
 */
@Service
public class UserFavouriteServiceImpl implements UserFavouriteService {
    //获取我喜欢的图片专辑
    @Autowired
    private UsersMapper userFavouriteMapper;
    @Autowired
    private FavoriteMapper favoriteMapper;

    public PageBean<UserFavouriteVO> Userfavourite(String userId) {

        PageHelper.startPage(PaginationContext.getPageNum(), PaginationContext.getPageSize());
        List<UserFavouriteVO> userFavouriteVOs = userFavouriteMapper.Userfavourite(userId);
        for (UserFavouriteVO userFavouriteVO : userFavouriteVOs) {
            userFavouriteVO.setOriginalUrl(AliOssUtils.getImageUrl(userFavouriteVO.getOriginalUrl()));//, new AliOssImageConfig("test")));
        }
        return new PageBean<UserFavouriteVO>(userFavouriteVOs);
    }

    @Override
    public int insert(Favorite favorite) {
        return favoriteMapper.insert(favorite);
    }


    public int deleteFavorite(List<String> imgId, String userId) {
        return favoriteMapper.deletedImage(imgId, userId);
    }


    public int deletedFavorite(String[] ids, int type, String userId) {
        Map<String, Object> idMap = new HashedMap();
        if (type == 0) {
            idMap.put("albIds", Arrays.asList(ids));
        } else {
            idMap.put("imgIds", Arrays.asList(ids));
        }
        idMap.put("userId", userId);
        return favoriteMapper.deletedFavorite(idMap);
    }

    @Override
    public Favorite findFavoriteByIds(Favorite favorite) {
        List<Favorite> favorites = favoriteMapper.getFavoriteByIds(favorite);
        if (favorites.size() > 0)
            return favorites.get(0);
        return null;
    }

    public int deleteAlbumFavorite(List<String> albId, String userId) {
        return favoriteMapper.deletedAlbum(albId, userId);

    }

}
